home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libd.lha / Lib / Des / EPC_ENCR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-19  |  4.8 KB  |  166 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/epc_encrypt.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * These routines perform encryption and decryption using the DES
  12.  * private key algorithm, or else a subset of it-- fewer inner loops.
  13.  * (AUTH_DES_ITER defaults to 16, may be less.)
  14.  *
  15.  * Under U.S. law, this software may not be exported outside the US
  16.  * without license from the U.S. Commerce department.
  17.  *
  18.  * These routines form the library interface to the des facilities.
  19.  *
  20.  * spm    8/85    MIT project athena
  21.  */
  22.  
  23. #ifndef    lint
  24. static char rcsid_epc_encrypt_c[] =
  25. "$Header: epc_encrypt.c,v 4.7 88/11/15 11:28:23 jtkohl Exp $";
  26. #endif    lint
  27.  
  28. #include <mit_copy.h>
  29. #include <des.h>
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #ifdef BSDUNIX
  34. #include <sys/time.h>
  35. #endif
  36.  
  37. extern int des_debug;
  38. extern int des_debug_print();
  39.  
  40. /*
  41.  * epc_encrypt is an "error propagation chaining" encrypt operation
  42.  * for DES, similar to CBC.  However, CBC limits errors to 2 blocks,
  43.  * while "epc" will propagate a single bit error anywhere in the chain
  44.  * all the way through to the end. This is NOT an official mode of
  45.  * operation.  It offers the cryptanalyst the same plaintext
  46.  * ciphertext pairs for repeated plaintext, regardless of the IV.
  47.  * However, cryptanalysis is not one of our concerns.
  48.  */
  49.  
  50. /*
  51.  * This routine performs the EPC error-propagation chaining operation,
  52.  * either encrypting from cleartext to ciphertext, if encrypt != 0, or
  53.  * decrypting from ciphertext to cleartext, if encrypt == 0.
  54.  *
  55.  * The key schedule is passed as an arg, as well as the cleartext or
  56.  * ciphertext. The cleartext and ciphertext should be in host order.
  57.  *
  58.  * NOTE-- the output is ALWAYS an multiple of 8 bytes long.  If not
  59.  * enough space was provided, your program will get trashed.
  60.  *
  61.  * For encryption, the cleartext string is null padded, at the end, to
  62.  * an integral multiple of eight bytes.
  63.  *
  64.  * For decryption, the ciphertext will be used in integral multiples
  65.  * of 8 bytes, but only the first "length" bytes returned into the
  66.  * cleartext.
  67.  */
  68.  
  69. int
  70. epc_encrypt(in,out,length,key,iv,encrypt)
  71.     des_cblock *in;        /* >= length bytes of inputtext */
  72.     des_cblock *out;        /* >= length bytes of outputtext */
  73.     register long length;    /* in bytes */
  74.     int encrypt;        /* 0 ==> decrypt, else encrypt */
  75.     des_key_schedule key;        /* precomputed key schedule */
  76.     des_cblock *iv;        /* 8 bytes of ivec */
  77. {
  78.     register unsigned long *input = (unsigned long *) in;
  79.     register unsigned long *output = (unsigned long *) out;
  80.     register unsigned long *ivec = (unsigned long *) iv;
  81.  
  82.     unsigned long i,j;
  83.     static unsigned long t_input[2];
  84.     static unsigned long t_output[2];
  85.     static unsigned char *t_in_p = (unsigned char *) t_input;
  86.     static unsigned long xor_0, xor_1;
  87.  
  88.     if (encrypt) {
  89.     xor_0 = *ivec++;
  90.     xor_1 = *ivec;
  91.  
  92.     for (i = 0; length > 0; i++, length -= 8) {
  93.         /* get input */
  94.         t_input[0] = *input;
  95.         t_input[1] = *(input+1);
  96.         /* zero pad */
  97.         if (length < 8)
  98.         for (j = length; j <= 7; j++)
  99.             *(t_in_p+j)= 0;
  100.  
  101. #ifdef DEBUG
  102.         if (des_debug)
  103.         des_debug_print("clear",length,t_input[0],t_input[1]);
  104. #endif
  105.         /* do the xor for cbc into the temp */
  106.         t_input[0] ^= xor_0 ;
  107.         t_input[1] ^= xor_1 ;
  108.         /* encrypt */
  109.         des_ecb_encrypt(t_input,t_output,key,encrypt);
  110.         /* copy temp output and save it for cbc */
  111.         *output++ = t_output[0];
  112.         *output++ = t_output[1];
  113.         xor_0 = *input++;
  114.         xor_1 = *input++;
  115. #ifdef DEBUG
  116.         if (des_debug) {
  117.         des_debug_print("xor'ed",i,t_input[0],t_input[1]);
  118.         des_debug_print("cipher",i,t_output[0],t_output[1]);
  119.         }
  120. #endif
  121.     }
  122.     t_output[0] = 0;
  123.     t_output[1] = 0;
  124.     xor_0 = 0;
  125.     xor_1 = 0;
  126.     return 0;
  127.     }
  128.  
  129.     else    /* decrypt */
  130.     {
  131.     xor_0 = *ivec++;
  132.     xor_1 = *ivec;
  133.  
  134.     for (i = 0; length > 0; i++, length -= 8) {
  135.         /* get input */
  136.         t_input[0] = *input++;
  137.         t_input[1] = *input++;
  138.         /* no padding for decrypt */
  139. #ifdef DEBUG
  140.         if (des_debug)
  141.         des_debug_print("cipher",i,t_input[0],t_input[1]);
  142. #endif
  143.         /* encrypt */
  144.         des_ecb_encrypt(t_input,t_output,key,encrypt);
  145. #ifdef DEBUG
  146.         if (des_debug)
  147.         des_debug_print("out pre xor",i,t_output[0],t_output[1]);
  148. #endif
  149.         /* do the xor for cbc into the output */
  150.         t_output[0] ^= xor_0 ;
  151.         t_output[1] ^= xor_1 ;
  152.         /* copy temp output */
  153.         *output++ = t_output[0];
  154.         *output++ = t_output[1];
  155.         /* save xor value for next round */
  156.         xor_0 = t_output[0];
  157.         xor_1 = t_output[1];
  158. #ifdef DEBUG
  159.         if (des_debug)
  160.         des_debug_print("clear",i,t_output[0],t_output[1]);
  161. #endif
  162.     }
  163.  
  164.     }
  165. }
  166.